package com.ctshk.app.order.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.cms.dto.BranchOfficeDTO;
import com.ctshk.rpc.cms.req.BOQueryReq;
import com.ctshk.rpc.cms.service.IBaiDuApiService;
import com.ctshk.rpc.order.goods.dto.CommodityInfoRespDTO;
import com.ctshk.rpc.order.goods.dto.RegionalFreightDTO;
import com.ctshk.rpc.order.goods.dto.resp.CommodityInfo;
import com.ctshk.rpc.order.goods.dto.resp.CommodityInfoResp;
import com.ctshk.rpc.order.goods.dto.resp.CommodityPageReq;
import com.ctshk.rpc.order.goods.req.CommodityReq;
import com.ctshk.rpc.order.goods.req.RegionalFreightReq;
import com.ctshk.rpc.order.goods.service.ICommodityHomeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * TODO
 * 商品首页展示 前端控制器
 * @author huangxiansheng
 * @date 2021/5/19 1:02 下午
 */
@Api(value = "商城C端首页",tags = "商城C端首页")
@RestController
@RequestMapping(value = "/commodity/home")
public class OrderCommodityHomeController extends BaseController {

    @DubboReference
    private ICommodityHomeService iCommodityHomeService;

    @DubboReference
    private IBaiDuApiService baiDuApiService;

    @ApiOperation(value = "C端获取所有门店并计算当前位置距离门店的距离", notes = "C端获取所有门店并计算当前位置距离门店的距离")
    @OperationLogAnnotation(operModul = OperationLogModel.CMS, operType = OperationLogType.QUERY)
    @PostMapping("/query")
    public Result<List<BranchOfficeDTO>> query(@RequestBody @Valid BOQueryReq req) {
        return baiDuApiService.query(req);
    }

    @ApiOperation(value = "商城首页列表聚合",notes = "商城首页列表聚合")
    @OperationLogAnnotation(operModul = OperationLogModel.GOODS, operType = OperationLogType.QUERY, operTitle = "首页商品推荐", operDesc = "首页商品推荐")
    @PostMapping("/indexList")
    public Result<CommodityInfoRespDTO> indexList(@RequestBody @Valid JSONObject req){
        if (null == req || "{}".equalsIgnoreCase(req.toString())){
            return Result.failed(SystemError.SYS_402);
        }
        String code = (String)req.get("code");
        Integer type = (Integer)req.get("type");
        Integer size = req.getInteger("size");
        return iCommodityHomeService.indexList(type,code,size);
    }

    @ApiOperation(value = "商城C端二级菜单展示", notes = "商城C端二级菜单展示")
    @OperationLogAnnotation(operModul = OperationLogModel.GOODS, operType = OperationLogType.QUERY, operTitle = "商城C端二级菜单展示", operDesc = "商城C端二级菜单展示")
    @GetMapping("/twoLevelAndApp")
    public Result twoLevelAndApp(){
        return iCommodityHomeService.twoLevelAndApp();
    }

    @ApiOperation(value = "首页商品推荐", notes = "首页商品推荐")
    @OperationLogAnnotation(operModul = OperationLogModel.GOODS, operType = OperationLogType.QUERY, operTitle = "首页商品推荐", operDesc = "首页商品推荐")
    @GetMapping("/recommend/{type}/{page}/{size}")
    public Result<PageResponse<CommodityInfoResp>> recommend(@PathVariable Integer type,@PathVariable Integer page,@PathVariable Integer size){

        Result<List<CommodityInfoResp>> listResult = iCommodityHomeService.commodityRecommend(type);
        IPage<CommodityInfoResp> commodityInfoRespIPage = new Page<>();
        commodityInfoRespIPage.setRecords(listResult.getData());
        PageResponse<CommodityInfoResp> response = new PageResponse<>();
        response.setIsFirst(commodityInfoRespIPage.getCurrent() == 1);
        response.setIsLast(commodityInfoRespIPage.getTotal() <= (commodityInfoRespIPage.getCurrent() * commodityInfoRespIPage.getSize()));
        response.setTotal(commodityInfoRespIPage.getTotal());
        response.setPageSize(size);
        response.setPageNo(commodityInfoRespIPage.getCurrent());
        response.setRecords(listResult.getData());
        return Result.success(response);
    }

    @ApiOperation(value = "首页数据初始化缓存", notes = "首页数据初始化缓存")
    @OperationLogAnnotation(operModul = OperationLogModel.GOODS, operType = OperationLogType.ADD, operTitle = "商品首页列表数据缓存", operDesc = "商品首页列表数据缓存")
    @PostMapping("/updateGoodsCache")
    public Result updateGoodsCache(@RequestBody CommodityPageReq pageReq){
        return iCommodityHomeService.updateGoodsCache(pageReq,getCurrentUser());
    }

    @ApiOperation(value = "首页级联菜单读取", notes = "首页级联菜单读取")
    @OperationLogAnnotation(operModul = OperationLogModel.GOODS, operType = OperationLogType.QUERY, operTitle = "首页级联分类读取", operDesc = "首页级联分类读取")
    @PostMapping("/getClassification")
    public Result getClassification(){
        return iCommodityHomeService.getClassification();
    }

    @ApiOperation(value = "首页专题读取", notes = "首页专题读取")
    @OperationLogAnnotation(operModul = OperationLogModel.GOODS, operType = OperationLogType.QUERY, operTitle = "首页专题读取", operDesc = "首页专题读取")
    @PostMapping("/getShoppingSpecial")
    public Result getShoppingSpecial(){
        return iCommodityHomeService.getShoppingSpecial();
    }

    @ApiOperation(value = "首页二级分类商品展示", notes = "首页二级分类商品展示")
    @OperationLogAnnotation(operModul = OperationLogModel.GOODS, operType = OperationLogType.QUERY, operTitle = "首页二级分类商品展示", operDesc = "首页二级分类商品展示")
    @PostMapping("/getTwoLevel")
    public Result getTwoLevel(@RequestBody @Valid CommodityReq req){
        return iCommodityHomeService.getTwoLevel(req.getTowLevelId(),req.getStatusCode(),req.getPage(),req.getSize());
    }

    @ApiOperation(value = "商品详情页展示", notes = "商品详情页展示")
    @OperationLogAnnotation(operModul = OperationLogModel.GOODS, operType = OperationLogType.QUERY, operTitle = "商品详情页展示", operDesc = "商品详情页展示")
    @GetMapping("/info")
    public Result<CommodityInfo> info(@RequestParam String id){
        return iCommodityHomeService.info(id,getCurrentUserAllowNotLogInUserId());
    }

    @ApiOperation(value = "查询地区运费模板", notes = "查询地区运费模板")
    @OperationLogAnnotation(operModul = OperationLogModel.GOODS, operType = OperationLogType.QUERY, operTitle = "查询地区运费模板", operDesc = "查询地区运费模板")
    @PostMapping("/region")
    public Result<RegionalFreightDTO> region(@RequestBody RegionalFreightReq req){
            return iCommodityHomeService.region(req);
    }

}
